package com.hanter.downloader;

import androidx.annotation.Nullable;

/**
 * 结果，以http状态码为依据，而 < 0 的为 自定义， - 1 取消 -2 中断 -3 下载失败 200 完成
 */
public class Response {

    /**
     * 取消/中断
     */
    public static final int ERROR_CANCELED = -1;
    /**
     * 失败
     */
    public static final int ERROR_FAILED = -2;

    private int code;
    private String message;
    @Nullable
    private Throwable throwable;

    public Response() {
    }

    public Response(int code) {
        this.code = code;
    }

    public Response(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response(int code, @Nullable Throwable throwable) {
        this.code = code;
        this.throwable = throwable;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Nullable
    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(@Nullable Throwable throwable) {
        this.throwable = throwable;
    }

    public boolean isSuccessful() {
        return code >= 200 && code < 300;
    }

    public boolean isCanceled() {
        return code == ERROR_CANCELED;
    }

}
